package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.R;
import com.atguigu.yygh.common.exception.ziDyException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-01
 */
@Api(tags = "医院数据设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院数据")
    @GetMapping("findAll")
    public R getAllHosp(){

//        try {
//            int i= 10/0;
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new ziDyException(500,"by/zero");
//        }

        List<HospitalSet> list = hospitalSetService.list();

        return R.ok().data("items",list);

    }

    @ApiOperation(value = "根据id逻辑删除数据")
    @DeleteMapping("deleteById/{id}")
    public R deleteId(@PathVariable String id){
        boolean b = hospitalSetService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }

    }

    /**
     * 分页查询,按照条件查询：方式1
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/selectHospPage/{current}/{limit}")
    public R selectHospPage(@PathVariable Long current , @PathVariable Long limit){

        Page<HospitalSet> page = new Page<>(current,limit);

        hospitalSetService.page(page);

        return R.ok().message("分页查询成功").data("total",page.getTotal()).data("items",page.getRecords());

    }

    /**
     * 分页查询，条件查询方式2
     */
    @ApiOperation(value = "按照条件分页查询")
    @PostMapping("/selectHospPageSet/{current}/{limit}")
    public R selectHospPageSet(@PathVariable Long current , @PathVariable Long limit,
                            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(current,limit);

        if(StringUtils.isEmpty(hospitalSetQueryVo)){
            hospitalSetService.page(page);
        }else{
            QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

            if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())){
                queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
            }
            if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
                queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
            }
            queryWrapper.orderByDesc("update_time");
            hospitalSetService.page(page,queryWrapper);
        }
        return R.ok().message("条件查询成功").data("total",page.getTotal()).data("items",page.getRecords());
    }

    /**
     * 新增数据
     */
    @ApiOperation(value = "新增一条数据")
    @PostMapping("/hospSave")
    public R hospSave(@RequestBody HospitalSet hospitalSet){

        boolean save = hospitalSetService.save(hospitalSet);

        if(save){
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }


    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询一条数据")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id){
        HospitalSet byId = hospitalSetService.getById(id);

        return R.ok().data("data",byId);
    }

    /**
     * 根据id修改数据
     */
    @ApiOperation(value = "根据id修改数据")
    @PostMapping("/updateHospById")
    public R updateHospById(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setUpdateTime(null);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "根据id批量删除数据")
    @DeleteMapping("deleteByIds")
    public R deleteByIds(@RequestBody List<Long> list){

        boolean b = hospitalSetService.removeByIds(list);
        if(b){
            return R.ok().message("批量删除成功");
        }else{
            return R.error().message("批量删除失败");
        }

    }

    /**
     * 锁定和解锁医院
     */
    @ApiOperation(value = "锁定和解锁医院状态")
    @PutMapping("/updateHospStatus/{id}/{status}")
    public R updateHospStatus(@PathVariable String id,@PathVariable Integer status){

        //首先根据id查询出数据
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置医院状态
        hospitalSet.setStatus(status);
        //修改医院状态
        hospitalSet.setUpdateTime(null);
        boolean b = hospitalSetService.updateById(hospitalSet);

        if(b){
            return R.ok().message("修改医院状态成功");
        }else{
            return R.error().message("修改医院状态失败");
        }

    }
}

